package com.delianna.bwt.data;

//Original class created by Sergi Juanola
//http://www.flexjockey.com/2011/03/create-a-pretty-simple-cache-for-android/

import java.io.File;

import android.os.Environment;

/**
 * @author Delio Castillo (jangeador@gmail.com)
 *
 */
public class Cache {
    private static CacheStore cache = CacheStore.getInstance();
    
    public static boolean isCacheAvailable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {
            return true;
        } else {
            return false;
        }
    }
    
    public static boolean isCacheWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        } else {
            return false;
        }
    }
    
    public static void saveCacheFile(String content) {
        if(!isCacheWritable()) return;
        cache.saveCacheFile(content);
    }
    
    public static File getCacheFile(String cacheUri) {
        if(!isCacheAvailable()) return null;
        File f = cache.getCacheFile(cacheUri);
        return f;
    }
    
    public static void saveCacheFileFromURL(String DownloadURL, String fileName) {
    	if(!isCacheWritable()) return;
        
        cache.saveCacheFileFromURL(DownloadURL, fileName);
    }
    
    public static boolean deleteCacheFile(File fileUri){
    	return cache.deleteCacheFile(fileUri);
    }
    
    public static void clearCache(){
    	cache.cleanCacheStart();
    }
}